package com.atbun.aclservice.controller;


import com.atbun.aclservice.entity.AclUser;
import com.atbun.aclservice.service.RoleService;
import com.atbun.aclservice.service.UserService;
import com.atbun.commonutils.MD5;
import com.atbun.commonutils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-05-23
 */
@RestController
@RequestMapping("/admin/acl/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    /**
     * 分页获取用户列表
     * @param page
     * @param limit
     * @param userVo 查询对象
     * @return
     */
    @GetMapping("queryAllUserPage/{page}/{limit}")
    public R queryAllUserPage(@PathVariable long page, @PathVariable long limit,AclUser userVo){
        Page<AclUser> pageParam = new Page<>(page, limit);
        QueryWrapper<AclUser> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(userVo.getUsername())) {
            wrapper.like("username",userVo.getUsername());
        }
        userService.page(pageParam, wrapper);
        return R.ok().data("items", pageParam.getRecords()).data("total", pageParam.getTotal());
    }

    /**
     * 新增管理用户
     * @param user
     * @return
     */
    @PostMapping("save")
    public R save(@RequestBody AclUser user) {
        user.setPassword(MD5.encrypt(user.getPassword()));
        userService.save(user);
        return R.ok();
    }

    /**
     * 修改管理用户
     * @param user
     * @return
     */
    @PutMapping("update")
    public R updateById(@RequestBody AclUser user) {
        userService.updateById(user);
        return R.ok();
    }

    /**
     * 删除管理用户
     * @param id
     * @return
     */
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable String id) {
        userService.removeById(id);
        return R.ok();
    }

    /**
     * 根据id列表删除管理用户
     * @param idList
     * @return
     */
    @DeleteMapping("batchRemove")
    public R batchRemove(@RequestBody List<String> idList) {
        userService.removeByIds(idList);
        return R.ok();
    }

    /**
     * 根据用户获取角色数据
     * @param userId
     * @return
     */
    @GetMapping("/toAssign/{userId}")
    public R toAssign(@PathVariable String userId) {
        Map<String, Object> roleMap = roleService.findRoleByUserId(userId);
        return R.ok().data(roleMap);
    }

    /**
     * 根据用户分配角色
     * @param userId
     * @param roleIds
     * @return
     */
    @PostMapping("/doAssign")
    public R doAssign(@RequestParam String userId,@RequestParam String[] roleIds) {
        roleService.saveUserRoleRealtionShip(userId,roleIds);
        return R.ok();
    }

    /**
     * 根据用户id获取用户数据
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public R getUserInfo(@PathVariable String id){
        AclUser aclUser = this.userService.getById(id);
        return R.ok().data("item",aclUser);
    }

}

